package org.example.myleet.p1652;

public class Solution {
    public int[] decrypt(int[] code, int k) {
        int[] result = new int[code.length];
        if (k > 0) {
            int sum = 0;
            for (int i = 1; i <= k; ++i) {
                sum += code[i];
            }
            result[0] = sum;
            for (int i = 1; i < code.length; ++i) {
                sum -= code[i];
                int idx = (i + k) % code.length;
                sum += code[idx];
                result[i] = sum;
            }
        } else if (k < 0) {
            k = -k;
            int sum = 0;
            for (int i = code.length - 2; i >= code.length - k - 1; --i) {
                sum += code[i];
            }
            result[code.length - 1] = sum;
            for (int i = code.length - 2; i >= 0; --i) {
                sum -= code[i];
                int idx = (i - k + code.length) % code.length;
                sum += code[idx];
                result[i] = sum;
            }
        }
        return result;
    }
}
